package com.takepoints21.mobile;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.takepoints.android.TPConst;
import com.takepoints.android.services.SecurityServices;

public class UserRegister extends BaseActivity {
	
	private String regUsrname;
	private String regPasswd;
	private int memId = -1;
	
	@Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        manageMenu(0);

        TextView lblHeader = (TextView)findViewById(R.id.lblHeader);
        TextView lblUsrname = (TextView)findViewById(R.id.lblUsrname);
    	TextView lblPasswd = (TextView)findViewById(R.id.lblPasswd);
    	
    	TextView lnkRegister = (TextView)findViewById(R.id.lnkRegister);
    	lnkRegister.setVisibility(TextView.INVISIBLE);
    	
    	lblHeader.setText(R.string.title_register);
    	lblUsrname.setText(R.string.regis_usrname);
    	lblPasswd.setText(R.string.regis_passwd);
    	
    	Button btnAccess = (Button)findViewById(R.id.btnAccess);
    	btnAccess.setText(R.string.btn_register);
    	btnAccess.setOnClickListener(new View.OnClickListener(){
			 public void onClick(View view) {
				 EditText txtUsrname = (EditText)findViewById(R.id.txtUsrname);
				 EditText txtPasswd = (EditText)findViewById(R.id.txtPasswd);
				 
				 regUsrname = txtUsrname.getText().toString().toLowerCase();
				 regPasswd = txtPasswd.getText().toString();
				 processRegister();
			 }
		 });
	}
	
	private void processRegister(){
		final ProgressDialog dialog = ProgressDialog.show(this, "Takepoints.com", "กรุณารอสักครู่ ...", true);
		final Handler handler = new Handler(){
			public void handleMessage(Message msg){
				if(memId > 0){
					registerSuccess();
				} else{
					alert("อีเมล์นี้ไม่สามารถสมัครได้\n\nกรุณาลองใหม่อีกครั้ง !");
				}
				dialog.dismiss();
			}
		};
		Thread thread = new Thread(){
        	public void run(){
        		memId = SecurityServices.registerUser(regUsrname, regPasswd);
        		handler.sendEmptyMessage(0);
        	}
		};
        thread.start();
	}
	
	private void registerSuccess(){
		initSharedPreference();
		if(sharePref != null && sharePref.edit() != null){
			Editor editor = sharePref.edit();
			editor.putString(TPConst.PRF_USRNAME, regUsrname);
			editor.putString(TPConst.PRF_PASSWD, regPasswd);
			editor.putInt(TPConst.PRF_MEMBER_ID, memId);
			editor.commit();
			popup("สมัครสมาชิกสำเร็จ !");
		} else{
			popup("สมัครสมาชิกสำเร็จ !\n\nสามารถ Login โดยใช้อีเมล์ที่ได้สมัครไว้");
		}
		Intent homeInt = new Intent(this, Takepoints.class);
		startActivityForResult(homeInt, 0);
	}
	
	@Override
	public void onBackPressed(){
		Intent homeInt = new Intent(this, Takepoints.class);
		startActivityForResult(homeInt, 0);
		return;
	}
	
}
